package com.example.hszpad.fragment;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.hszpad.R;
import com.example.hszpad.adapter.BaseRecyclerAdapter;
import com.example.hszpad.adapter.SmartViewHolder;
import com.example.hszpad.bean.MenuItemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HSZViewFragmentAssist extends Fragment {


    private ImageView iv_assist;
    private RecyclerView recyclerView;
    private BaseRecyclerAdapter<MenuItemBean> recyclerChildAdapter;
    private List<MenuItemBean> menuItemBeanChildList;
    private MenuItemBean curChildBean;
    private AbsoluteLayout fl_content;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_hszview_fragment_assist, container, false);
        iv_assist = view.findViewById(R.id.iv_assist);
        fl_content = view.findViewById(R.id.fl_content);
        recyclerView = view.findViewById(R.id.recyclerView);
        AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(
                1632,
                1062,
                0,
                0);
        iv_assist.setLayoutParams(layoutParams);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        menuItemBeanChildList = new ArrayList<>();
        MenuItemBean bean = new MenuItemBean(1, "燃油输送", 3, R.drawable.ic_home_white_24dp, true);
        menuItemBeanChildList.add(bean);
//        bean = new MenuItemBean(2, "滑油循环", 3, R.drawable.ic_home_white_24dp, false);
//        menuItemBeanChildList.add(bean);
        bean = new MenuItemBean(3, "冷却系统", 3, R.drawable.ic_home_white_24dp, false);
        menuItemBeanChildList.add(bean);
        bean = new MenuItemBean(4, "电机管理", 3, R.drawable.ic_home_white_24dp, false);
        menuItemBeanChildList.add(bean);
        bean = new MenuItemBean(5, "压载水", 3, R.drawable.ic_home_white_24dp, false);
        menuItemBeanChildList.add(bean);
        bean = new MenuItemBean(6, "生活保障装备", 3, R.drawable.ic_home_white_24dp, false);
        menuItemBeanChildList.add(bean);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerChildAdapter = new BaseRecyclerAdapter<MenuItemBean>(menuItemBeanChildList, R.layout.layout_view_menu_item, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MenuItemBean itemBean = menuItemBeanChildList.get(position);
                if (itemBean.isSelect()) {
                    itemBean.setSelect(false);
                    curChildBean = null;
                } else {
                    for (int i = 0; i < menuItemBeanChildList.size(); i++) {
                        menuItemBeanChildList.get(i).setSelect(false);
                    }
                    itemBean.setSelect(true);
                    if (itemBean != curChildBean) {
                        switch (itemBean.getId()) {
                            case 1:
                                Glide.with(getActivity()).load(R.drawable.ic_content_assist1).into(iv_assist);
                                break;
                            case 3:
                                Glide.with(getActivity()).load(R.drawable.ic_content_assist2).into(iv_assist);
                                break;
                            case 4:
                                Glide.with(getActivity()).load(R.drawable.ic_content_assist3).into(iv_assist);
                                break;
                            case 5:
                                Glide.with(getActivity()).load(R.drawable.ic_content_assist4).into(iv_assist);
                                break;
                            case 6:
                                Glide.with(getActivity()).load(R.drawable.ic_content_assist5).into(iv_assist);
                                break;
                        }
                        curChildBean = itemBean;
                    }
                }
                recyclerChildAdapter.notifyDataSetChanged();
                recyclerChildAdapter.notifyListDataSetChanged();
            }
        }) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, MenuItemBean model, int position) {
                holder.text(R.id.tv_title, model.getName());
                if (model.isSelect()) {
                    holder.viewBgImageId(R.id.ll_content, R.drawable.ic_bg_menu_pressed);
                    holder.textColorId(R.id.tv_title, R.color.colorSecondMenuNormal);
                } else {
                    holder.viewBgImageId(R.id.ll_content, R.drawable.ic_bg_menu_normal);
                    holder.textColorId(R.id.tv_title, R.color.colorSecondMenuPressed);
                }
            }
        };
        recyclerView.setAdapter(recyclerChildAdapter);
        curChildBean = menuItemBeanChildList.get(0);
        Glide.with(getActivity()).load(R.drawable.ic_content_assist1).into(iv_assist);
        return view;
    }

}
